// ParseParam 将 a=1 转成 对象形式
type ParseParam<StrParam extends string> = StrParam extends `${infer Key}=${infer Value}`
  ? {
      [K in Key]: Value;
    }
  : {};

// MergeParams
// prettier-ignore
type MergeParams<OneParam extends Record<string, any>, OtherParam extends Record<string, any>> = {
  [Key in keyof OneParam | keyof OtherParam]: 
    Key extends keyof OneParam
      // 如果 这个key 分别存在于OneParam与OtherParam 那么需要将这两个值构造成一个数组类型
      ? Key extends keyof OtherParam
        ? MergeValues<OneParam[Key], OtherParam[Key]>
        : OneParam[Key]
      : Key extends keyof OtherParam
        ? OtherParam[Key]
        : never;
};

// prettier-ignore
type MergeValues<One, Other> = One extends Other
  ? One
  : Other extends unknown[]
    ? [One, ...Other]
    : [One, Other];

// 以 & 进行拆分，然后递归拆分，通过ParseParam转换拆分出来的类型 最后合并
type ParseQueryString<Str extends string> = Str extends `${infer Param}&${infer Rest}`
  ? MergeParams<ParseParam<Param>, ParseQueryString<Rest>>
  : ParseParam<Str>;

type res = ParseQueryString<'a=1&a=2&a=0&b=2&c=3'>;

export {};
